package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.CommonUserInfo;
import com.ruoyi.system.mapper.CommonUserMapper;
import com.ruoyi.system.domain.CommonUser;
import com.ruoyi.system.service.ICommonUserService;

/**
 * 普通用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class CommonUserServiceImpl implements ICommonUserService 
{
    @Autowired
    private CommonUserMapper commonUserMapper;

    /**
     * 查询普通用户
     * 
     * @param id 普通用户主键
     * @return 普通用户
     */
    @Override
    public CommonUser selectCommonUserById(Long id)
    {
        return commonUserMapper.selectCommonUserById(id);
    }

    /**
     * 查询普通用户列表
     * 
     * @param commonUser 普通用户
     * @return 普通用户
     */
    @Override
    public List<CommonUser> selectCommonUserList(CommonUser commonUser)
    {
        return commonUserMapper.selectCommonUserList(commonUser);
    }

    /**
     * 新增普通用户
     * 
     * @param commonUser 普通用户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCommonUser(CommonUser commonUser)
    {
        commonUser.setCreateTime(DateUtils.getNowDate());
        int rows = commonUserMapper.insertCommonUser(commonUser);
        insertCommonUserInfo(commonUser);
        return rows;
    }

    /**
     * 修改普通用户
     * 
     * @param commonUser 普通用户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCommonUser(CommonUser commonUser)
    {
        commonUser.setUpdateTime(DateUtils.getNowDate());
        commonUserMapper.deleteCommonUserInfoByUserId(commonUser.getId());
        insertCommonUserInfo(commonUser);
        return commonUserMapper.updateCommonUser(commonUser);
    }

    /**
     * 批量删除普通用户
     * 
     * @param ids 需要删除的普通用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCommonUserByIds(Long[] ids)
    {
        commonUserMapper.deleteCommonUserInfoByUserIds(ids);
        return commonUserMapper.deleteCommonUserByIds(ids);
    }

    /**
     * 删除普通用户信息
     * 
     * @param id 普通用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCommonUserById(Long id)
    {
        commonUserMapper.deleteCommonUserInfoByUserId(id);
        return commonUserMapper.deleteCommonUserById(id);
    }

    /**
     * 新增普通用户详情信息
     * 
     * @param commonUser 普通用户对象
     */
    public void insertCommonUserInfo(CommonUser commonUser)
    {
        List<CommonUserInfo> commonUserInfoList = commonUser.getCommonUserInfoList();
        Long id = commonUser.getId();
        if (StringUtils.isNotNull(commonUserInfoList))
        {
            List<CommonUserInfo> list = new ArrayList<CommonUserInfo>();
            for (CommonUserInfo commonUserInfo : commonUserInfoList)
            {
                commonUserInfo.setUserId(id);
                list.add(commonUserInfo);
            }
            if (list.size() > 0)
            {
                commonUserMapper.batchCommonUserInfo(list);
            }
        }
    }
}
